﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace Runda.EShop.Helper.GenPageFunc
{
    class Program
    {
        static Dictionary<int, string> Funs = new Dictionary<int, string>();

        static void Main(string[] args)
        {
            StringBuilder s = new StringBuilder();
            s.AppendLine("Delete from Sys_Page_Info");
            s.AppendLine();
            s.Append(GenerateFunc());

            string Path = Environment.CurrentDirectory.Replace("SourceCode\\Runda.EShop.Helper.GenPageFunc\\bin\\Debug", "");
            File.WriteAllText(Path + "DB\\初始化\\InitPageInfo.sql", s.ToString(), Encoding.UTF8);

            //Database db = DatabaseFactory.CreateDatabase();
            //DbCommand dbCommand = db.GetSqlStringCommand(s.ToString());
            //db.ExecuteNonQuery(dbCommand);
        }
        #region Function
        static string GenerateFunc()
        {
            List<string> OutPut = new List<string>();

            XmlDocument doc = new XmlDocument();
            doc.Load("../../PageInfo.xml");

            XmlNodeList nl = doc.GetElementsByTagName("Root");
            foreach (XmlNode node in nl[0].ChildNodes)
            {
                FindChild(ref OutPut, node, "NULL");
            }

            StringBuilder sb = new StringBuilder();
            foreach (string str in OutPut)
            {
                sb.AppendLine(str);
            }
            sb.AppendLine("");
            return sb.ToString();
        }
        static void FindChild(ref List<string> output, XmlNode node, string ParentCode)
        {
            output.Add(MakeSqlString(node, ParentCode));

            foreach (XmlNode n in node.ChildNodes)
            {
                FindChild(ref output, n, node.Attributes["PageCode"].Value.ToString());
            }
        }
        static string MakeSqlString(XmlNode node, string Parent)
        {
            string SQL = "INSERT INTO [Sys_Page_Info] ([PageID],[ParentPageID],[PageName],[IsMenu],[URL],deleted)  VALUES";
            string FuncId = "'" + node.Attributes["PageCode"].Value.ToString() + "'";
            string FFuncId = "'" + Parent + "'";
            if (Parent.ToLower() == "null")
            {
                FFuncId = "NULL";
            }
            string FuncName = "N'" + node.Attributes["Name"].Value.ToString() + "'";
            string m = node.Attributes["IsMenu"].Value.ToString() == "true" ? "1" : "0";
            string IsMenu = "'" + m + "'";
            string URL = "'" + node.Attributes["URL"].Value.ToString() + "'";

            string sfun = node.Attributes["PageCode"].Value.ToString();

            int funid = Convert.ToInt32(sfun.Replace("Page", ""));
            Funs.Add(funid, FuncId);

            string SQL2 = "(" + FuncId + "," + FFuncId + "," + FuncName + "," + IsMenu + ","
                            + URL + ",0)";



            return SQL + SQL2;
        }
        #endregion
         
    }
}
